﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Globalization;
using System.Web.Routing;

namespace MvcControlToolkit
{
    /// <summary>
    /// Calendar is an ASP.NET AJAX extender that can be attached to any ASP.NET 
    /// TextBox control. It provides client-side date-picking functionality with 
    /// customizable date format and UI in a popup control. You can interact with 
    /// the calendar by clicking on a day to set the date, or the "Today" link to 
    /// set the current date.
    /// </summary>
    public static class CalendarExtensions
    {
        private static readonly string DEFAULT_FORMAT = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;
        private static readonly CalendarPosition DEFAULT_POSITION = CalendarPosition.BottomLeft;

        /// <summary>
        /// Creates a Calendar extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the TextBox to extend with the calendar</param>
        public static string Calendar(this AjaxHelper helper, string elementId)
        {
            return helper.Calendar(elementId, DEFAULT_FORMAT);
        }

        /// <summary>
        /// Creates a Calendar extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the TextBox to extend with the calendar</param>                
        /// <param name="dateFormat">Format string used to display the selected date. Default is the ShortDateFormat for the Current UI Culture</param>        
        public static string Calendar(this AjaxHelper helper, string elementId, string dateFormat)
        {
            return helper.Calendar(elementId, "", dateFormat);
        }

        /// <summary>
        /// Creates a Calendar extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the TextBox to extend with the calendar</param>        
        /// <param name="popupButtonId">The ID of a control to show the calendar popup when clicked. If this value is not set, the calendar will pop up when the textbox receives focus</param>
        /// <param name="dateFormat">Format string used to display the selected date. Default is the ShortDateFormat for the Current UI Culture</param>        
        public static string Calendar(this AjaxHelper helper, string elementId, string popupButtonId, string dateFormat)
        {
            return helper.Calendar(elementId, "", DEFAULT_FORMAT, popupButtonId, DEFAULT_POSITION, DateTime.MinValue);
        }

        /// <summary>
        /// Creates a Calendar extender with the specified properties
        /// </summary>        
        /// <param name="elementId">The ID of the TextBox to extend with the calendar</param>
        /// <param name="cssClass">Name of the CSS class used to style the calendar. See the Calendar Theming section for more information</param>
        /// <param name="dateFormat">Format string used to display the selected date. Default is the ShortDateFormat for the Current UI Culture</param>
        /// <param name="popupButtonId">The ID of a control to show the calendar popup when clicked. If this value is not set, the calendar will pop up when the textbox receives focus</param>
        /// <param name="popupPosition">Indicates where the calendar popup should appear at the BottomLeft(default), BottomRight, TopLeft, TopRight, Left or Right of the TextBox</param>
        /// <param name="selectedDate">Indicates the date the Calendar extender is initialized with</param>
        public static string Calendar(this AjaxHelper helper, string elementId, string cssClass, string dateFormat, string popupButtonId, CalendarPosition popupPosition, DateTime selectedDate)
        {
            RouteValueDictionary props = new RouteValueDictionary();
            props.AddIfNotDefault("CssClass", cssClass);
            props.AddIfNotEqual("DateFormat", dateFormat, DEFAULT_FORMAT);            
            props.AddIfNotEqual("PopupPosition", popupPosition, DEFAULT_POSITION);
            props.AddIfNotEqual("SelectedDate", selectedDate, DateTime.MinValue);

            if(!string.IsNullOrEmpty(popupButtonId))
                props.Add("button", new ElementReference(popupButtonId));

            return helper.Calendar(elementId, props);
        }

        /// <summary>
        /// Creates a Calendar extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the TextBox to extend with the calendar</param>
        /// <param name="properties">An object containing the properties for this extender</param>
        /// <returns></returns>
        public static string Calendar(this AjaxHelper helper, string elementId, object properties)
        {
            var sb = new StringBuilder();
                        
            // Add Microsoft Ajax library   
            sb.Append(helper.MicrosoftAjaxInclude());

            // Add toolkit scripts   
            sb.Append(helper.ToolkitInclude(
                    "AjaxControlToolkit.ExtenderBase.BaseScripts.js",
                    "AjaxControlToolkit.Common.Common.js",
                    "AjaxControlToolkit.Common.DateTime.js",
                    "AjaxControlToolkit.Animation.Animations.js",
                    "AjaxControlToolkit.PopupExtender.PopupBehavior.js",
                    "AjaxControlToolkit.Animation.AnimationBehavior.js",
                    "AjaxControlToolkit.Common.Threading.js",
                    "AjaxControlToolkit.Compat.Timer.Timer.js",
                    "AjaxControlToolkit.Calendar.CalendarBehavior.js"
                ));

            // Add Calendar CSS file   
            sb.AppendLine(helper.DynamicToolkitCssInclude("AjaxControlToolkit.Calendar.Calendar.css"));

            // Perform $create               
            sb.AppendLine(helper.Create("AjaxControlToolkit.CalendarBehavior", properties, elementId));

            return sb.ToString();
        }
    }

    #region [Enumerations]

    public enum CalendarPosition
    {
        BottomLeft = 0,
        BottomRight = 1,
        TopLeft = 2,
        TopRight = 3,
        Right = 4,
        Left = 5
    }

    #endregion
}
